// pages/question/question_detail.js
const app = getApp()
const util = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    host:app.globalData.host,
    login:'',
  },
  ViewImage(e) {
    var array = this.data.data.image_array,arr = [];
    for(var i = 0;i<array.length;i++){
      arr[i] = this.data.host+array[i];
    }
    wx.previewImage({
      urls: arr,
      current: e.currentTarget.dataset.url
    });
  },
  /**
   * 点赞/取消点赞
   * @param {*} e 
   */
  thumb:function(e){
    var that = this;
    util.request('thumb/save', {type:2,related_id:that.data.id}, function (data) {
      if(data.code == 1){
        that.loadData(that.data.id);
      }else{
        util.fail(data.msg,that);
      }
    });
  },

    /**
   * 收藏/取消收藏
   * @param {*} e 
   */
  collect:function(e){
    var that = this;
    util.request('collect/save', {related_id:that.data.id}, function (data) {
      if(data.code == 1){
        that.loadData(that.data.id);
      }else{
        util.fail(data.msg,that);
      }
    });
  },

  loadData:function(id){
    var that = this;
    util.request('question/detail', {id:id}, function (data) {
      if(data.code == 1){
        that.setData({
          data:data.data.data,
        });
      }else{
        util.fail(data.msg,that);
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var id = e.id,that = this;
    that.setData({
      id:id
    });
    that.loadData(id);
    if(e.message_id){
      util.request('message/read', {id:e.message_id}, function (data) {
        console.log(data);
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      login:app.globalData.login,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})